import ajax, { ListResult, toOptions } from '../ajax';
import { computed, ref } from 'vue';

export function getExamsByOrganization(id: number) {
  return ajax.get<ListResult>(`/Problems/Organizations/${id}/Exams`);
}

export function getList(data: any) {
  return ajax.post<ListResult>('/Problems/Exams/Query', data);
}

export function updateEntity(data: any) {
  if (data.examId) {
    return ajax.put<any>('/Problems/Exams', data);
  } else {
    return ajax.post<any>('/Problems/Exams', data);
  }
}

export function deleteEntity(id: number) {
  let deleteCommand = { id: id };
  return ajax.put<any>('/Problems/Exams/Actions/Delete', deleteCommand);
}

export function updateExamTypeRelations(command: any) {
  return ajax.post<any>(
    '/Problems/Exams/{examId}/TypeRelations/Actions/Update',
    command
  );
}

export function getExamOptions() {
  let opts = ref([] as SelectOptionData[]);
  setTimeout(async () => {
    let data = await getExamIdAndName() as SelectOptionData[];
    opts.value = data;
  }, 1);

  return opts;
}

export async function getExamIdAndName() {
  let data = await getExamsByOrganization(1);
  return data.filter(a=>a.enabled).map(a=>{
    return {
      label: a.examName,
      value: a.examId
    };
  }) as SelectOptionData[];
}


